package com.plian.system.controller.transfer;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.Tools.DateUtil;
import com.plian.Tools.base.BaseController;
import com.plian.system.config.AppKeySign;
import com.plian.system.dto.transfer.DataPlatSign;
import com.plian.system.entity.fr.bank.BankKey;
import com.plian.system.entity.pm.property.PropertyBasics;
import com.plian.system.entity.zzd.ReportBalance;
import com.plian.system.mapper.zzd.ReportBalanceMapper;
import com.plian.system.model.R;
import com.plian.system.model.token.PassToken;
import com.plian.system.service.fr.bank.IBankKeyService;
import com.plian.system.service.pm.property.IPropertyBasicsService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2023/5/24 5:42 PM
 * @Param
 * @return
 * @Version: 1.0
 **/
@RestController
@RequestMapping("api/reportBalance")
@AllArgsConstructor
@Api(value = "银行余额-输出", tags = "银行余额输出")
@Slf4j
public class ReportBalanceController extends BaseController{

    private IBankKeyService bankKeyService;

    private ReportBalanceMapper reportBalanceMapper;

    private IPropertyBasicsService propertyBasicsService;

    @PostMapping(value = "/report-balance")
    @PassToken
    @AppKeySign
    public R<List<ReportBalance>> getReportBalance(@RequestBody DataPlatSign dataPlatSign){
        log.debug("token:" + dataPlatSign.getToken());
        String appId = dataPlatSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        PropertyBasics groupPro = propertyBasicsService.getOne(Wrappers.<PropertyBasics>lambdaQuery().eq(PropertyBasics::getOrgId, topGroupId));
        List<ReportBalance> reportBalances = new ArrayList<>();
        if (Optional.ofNullable(dataPlatSign.getEndTime()).isPresent()){
            String startTime = DateUtil.format(dataPlatSign.getStartTime(), DateUtil.PATTERN_DATE);
            String endTime = DateUtil.format(DateUtil.minusMillis(DateUtil.plusDays(dataPlatSign.getEndTime(), 1), 1), DateUtil.PATTERN_DATE);
           reportBalances = reportBalanceMapper.selectList(Wrappers.<ReportBalance>lambdaQuery().eq(ReportBalance::getReportCorpCode,groupPro.getAicOrgCode()).between(ReportBalance::getBalDate,startTime,endTime));
        }else {
            reportBalances = reportBalanceMapper.selectList(Wrappers.<ReportBalance>lambdaQuery().eq(ReportBalance::getReportCorpCode,groupPro.getAicOrgCode()));
        }
        log.info(dataPlatSign.getEndTime() + "--存款余额数量" + reportBalances.size());
        return R.data(reportBalances);
    }



}
